﻿using CPPEI.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestTCPClient
{
    class Program
    {
        static AppTCPClient _client;

        static void Main(string[] args)
        {
            _client = new AppTCPClient(new CallbackHandlerFactory());

            _client.Connect("127.0.0.1", 4788);
            App app = new App();
            app.Register("send", new SendCmd(_client));
            app.Run(args);
            _client.DisConnect();
        }
    }

    class App : XApplication
    {
        public App()
            : base("CPPEI", "Test", "TestTCPClient", "TCPClient测试程序")
        {
        }

        protected override bool OnXStartup(StartupParamCollection startupParams)
        {
            return true;
        }
    }

    class AppTCPClient : CPPEI.Net.TCP.TCPClient
    {
        public AppTCPClient(CPPEI.Net.TCP.ICallbackHandlerFactory responseHandlerFactory)
            : base(responseHandlerFactory)
        {
        }

        protected override void OnConnected()
        {
        }

        protected override void OnDisconnected()
        {
        }
    }

    class SendCmd : ICommand
    {
        CPPEI.Net.TCP.TCPClient _client;

        public SendCmd(CPPEI.Net.TCP.TCPClient client)
        {
            _client = client;
        }

        public void Execute(string cmd, string[] args)
        {
            TestTCPContract.DrawReq model = new TestTCPContract.DrawReq();
            model.Message = "我是消息长度最大32";
            model.Value = DateTime.Now.Second;
            model.Data = new byte[102400];
            model.Data[0] = 0xFF;
            model.Data[102309] = 0x01;

            _client.Send(new CPPEI.Net.Package(TestTCPContract.Scenario.Scene1,
                TestTCPContract.Command.Scene1.Cmd1,
                CPPEI.Net.BufferUtil.ToData(model)));
        }
    }

}
